package top.malaoshi.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.malaoshi.entity.Goods;
import top.malaoshi.entity.Orders;
import top.malaoshi.entity.UserModel;
import top.malaoshi.mapper.IGoodsMapper;
import top.malaoshi.mapper.IOrdersMapper;
import top.malaoshi.utils.UUIDTool;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderSrvImpl {

    @Resource
    private IOrdersMapper dao;

    @Resource
    private IGoodsMapper goodsDao;

    @Transactional
    public void save(Orders orders, UserModel um){
//        String id=new UUIDTool().getUUID();
        String id=UUIDTool.getUUID();
        Date date=new Date();
        orders.setId(id);
        orders.setUserId(um.getId());
        orders.setStatus(1);
        orders.setCreateTime(date);
        orders.setUpdateTime(date);

        Goods goods=goodsDao.showById(orders.getGoodsId());
        orders.setGoodsName(goods.getName());
        orders.setGoodsDescrip(goods.getDescrip());
        orders.setPrice(goods.getPrice());
        orders.setTotalPrice(orders.getPrice()*orders.getPersonNum());

        dao.save(orders);
        goodsDao.updatePsersonNumById(orders.getGoodsId(),orders.getPersonNum());

    }
}
